package org.jash.demo2205.shop

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.jash.demo2205.mvvm.BaseViewModel
import org.jash.demo2205.utils.SingleLiveData

class MineViewModel:BaseViewModel() {
    val uploadIconLiveData by lazy { SingleLiveData<String>() }
    fun uploadIcon(bytes: ByteArray) {
        viewModelScope.launch {
            try {
                val res = service.uploadIcon(MultipartBody.Part.createFormData("file", "1.1", bytes.toRequestBody()))
                if (res.code == 200) {
                    uploadIconLiveData.postValue(res.data)
                } else {
                    throw RuntimeException(res.message)
                }
            } catch (e: Exception) {
                failureLiveData.postValue(e)
            }
        }
    }

}